//
//  WelcomeViewController.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/6.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    lazy var iconImage:UIImageView = {
        //初始化为默认头像
        let icon=UIImageView(image: UIImage(named: "avatar_default_big"))
        //裁剪为圆形
        icon.layer.cornerRadius=50
        icon.layer.masksToBounds=true
        return icon
    }()
    lazy var nameLabel:UILabel = {
        let name = UILabel()
        name.text="欢迎回来!"
        //默认不显示
        name.alpha = 0
        return name
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        print("来到欢迎页面")
        setupUI()
        if let iconURL=UserAccountViewModel().account?.avatar_large{
            //如果能获取到用户头像的网址
            iconImage.sd_setImageWithURL(NSURL(string: iconURL), placeholderImage: UIImage(named: "avatar_default_big"))
            //placeholderImage的用处是保持形状
        }
        
        if let userName=UserAccountViewModel().account?.name{
            //如果能获取到用户昵称
            nameLabel.text="欢迎回来!慕云浮"
        }

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setupUI(){
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        view.addSubview(iconImage)
        view.addSubview(nameLabel)
        iconImage.snp_makeConstraints(closure: { (make) in
            //垂直居中
            make.centerX.equalTo(view.snp_centerX)
            //距离上部200
            make.top.equalTo(view.snp_top).offset(200)
            //大小100*100
            make.size.equalTo(CGSizeMake(100, 100))
        })
        
        nameLabel.snp_makeConstraints { (make) in
            //垂直居中
            make.centerX.equalTo(iconImage)
            //距离头像底部14
            make.top.equalTo(iconImage.snp_bottom).offset(14)
        }
        
    }
    // MARK: - 在这里执行动画,不然还没出来动画就结束了
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //约束更新,变为100px
        iconImage.snp_updateConstraints { (make) in
            make.top.equalTo(view.snp_top).offset(100)
        }
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: {
            self.view.layoutIfNeeded()
            }) { (Bool) in
                UIView.animateWithDuration(0.6, animations: {
                    //用户名称显示
                    self.nameLabel.alpha=1
                    }, completion: { (Bool) in
                        //标签显示完成之后
                        UIView.animateWithDuration(0.5, animations: {
                            //界面渐渐消失
                            self.view.alpha=0
                            }, completion: { (Bool) in
                                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: nil)
                        })
                })
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
